const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

const getLabelByValue = (options, value) => {
  const findOption = options.find(option => option.value === value)
  return findOption?.label || ''
}

/**
 * 移除对象数据中值为null或undefined的属性
 * @param {object} obj 对象数据
 */
export function omitNilValue(obj) {
  const newObj = {};
  for (let key of Object.keys(obj)) {
    if (obj[key] != undefined) {
      newObj[key] = obj[key]
    }
  }
  return newObj;
}

module.exports = {
  formatTime,
  getLabelByValue,
  omitNilValue
}
